package org.example.movies.controller;


import com.alibaba.fastjson.JSONObject;
import org.example.movies.pojo.TFilm;
import org.example.movies.service.DetailService;
import org.example.movies.service.impl.DetailServiceImpl;
import org.example.movies.utils.ResponseEntity;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/detail")
public class DetailController extends HttpServlet {
    private DetailService service = new DetailServiceImpl();
    public static final String CONTENT_TYPE_JSON = "application/json; charset=utf-8";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ResponseEntity<TFilm> entity = new ResponseEntity<TFilm>();
        try {
            String filmId = req.getParameter("filmId");
            TFilm film = service.getDetail(filmId);
            entity.setData(film);
            entity.setStatus(200);
            entity.setMsg("success");
            resp.setContentType(CONTENT_TYPE_JSON);
        } catch (Exception ex) {
            entity.setStatus(404);
            entity.setMsg("error");
        }
        resp.getWriter().println(JSONObject.toJSONString(entity));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }

}
